#include "JConfig.h"
#include "JVerifyValidate.h"
#include "JLog.h"
#include "JFile.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <string>
#include <boost/foreach.hpp>

//shared_ptr<JConfig> JConfig::instance;
std::map<std::string,std::string> JConfig::config;

JConfig::JConfig()
{

}

JConfig::~JConfig(void)
{

}

//shared_ptr<JConfig> JConfig::Instance()
//{
//	if(instance.get() == NULL)
//	{
//		instance.reset(new JConfig);
//	}
//	return instance;
//}

bool JConfig::Init( char* configFileName )
{
    
	if(JFile::IsExist(std::string(configFileName)))
	{
		ReadConfig(configFileName);
		//JLogger::Init();
		return true;
	}
	else
	{
		//JLogger::Init(".");
		return false;
	}
}

void JConfig::ReadConfig( char* configFileName )
{
	using boost::property_tree::ptree;
	ptree pt;

	read_xml(configFileName, pt,boost::property_tree::xml_parser::trim_whitespace);
	//std::string s = pt.get<std::string>("Configuration.LogFileDir");
	BOOST_FOREACH(ptree::value_type &v,pt.get_child("Configuration"))
	{
		PutConfig(v.first,v.second.data());
	}
	//config;
}

void JConfig::PutConfig( std::string key,std::string value )
{
	config[key] = value;
}

std::string JConfig::GetConfig( std::string s )
{
	std::map<std::string,std::string>::iterator ite = config.find(s);
	if(ite == config.end())
	{
		std::string tmp;
		return tmp;
	}
	else
	{
		return ite->second;
	}
}